package com.ruiao.tools.dongtaiguankong;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import androidx.core.app.ActivityCompat;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.ruiao.tools.R;
import com.ruiao.tools.utils.ToastHelper;

import com.ruiao.tools.dongtaiguankong.DongtaiBaojing.TaskBean;
import java.io.File;
import java.util.ArrayList;



public class StartTaskActivity extends BaseAppCompatActivity implements View.OnClickListener {
    private TaskBean bean;
    private boolean hasphoto = false;
    int i;
    private ImageView img1, img2, img3, img4, img5;
    private TextView tv_renwu, tv_time, tv_people, tv_address;

    private ArrayList<Uri> imgList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        bean = (TaskBean) getIntent().getSerializableExtra("task");
        imgList = new ArrayList<>();
        File file = new File(Environment.getExternalStorageDirectory(), "/yunwei/" + "xxx" + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        LocationUtils.getInstance(this).removeLocationUpdatesListener();
    }

    private void initData() {

        tv_time.setText(bean.time);
        tv_people.setText(bean.people);
        tv_address.setText(bean.company);

        File file1 = new File(Environment.getExternalStorageDirectory(), "/yunwei/" + 1 + ".jpg");
        File file2 = new File(Environment.getExternalStorageDirectory(), "/yunwei/" + 2 + ".jpg");
        imgList.add(Uri.fromFile(file1));
        imgList.add(Uri.fromFile(file2));
        if (file1.exists()) {
            file1.delete();
        }
        if (file2.exists()) {
            file2.delete();
        }


    }

    @Override
    protected void initView() {
        img1 = findViewById(R.id.img1);
        img1.setOnClickListener(this);
        tv_renwu = findViewById(R.id.tv_renwuhao);
        tv_time = findViewById(R.id.tv_time);
        tv_people = findViewById(R.id.tv_people);
        tv_address = findViewById(R.id.tv_address);
    }

    @Override
    public String SetTitle() {
        return "拍照开始任务";

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_task;
    }

    @Override
    public void onClick(View view) {





    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_SET_REQUEST_CODE) {
            if (lacksPermissions(PERMISSION)) {

//                Log.i(TAG, "缺少权限去申请");

                ActivityCompat.requestPermissions(this, PERMISSION, OPEN_SET_REQUEST_CODE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == OPEN_SET_REQUEST_CODE) {

            if (hasAllPermissionsGranted(grantResults)) {

//                Log.i(TAG, "用户允许打开权限");
//                updata();
            } else {

//                Log.i(TAG, "用户拒绝打开权限");

                showPermissionDialog("请去设置页面打开权限", OPEN_SET_REQUEST_CODE);
            }

        }


    }



    public void go(View view) {
        if (!hasphoto) {
            ToastHelper.shortToast(context, "请拍摄照片记录");
            return;
        }
        if (lacksPermissions(PERMISSION)) {

            ActivityCompat.requestPermissions(this, PERMISSION, OPEN_SET_REQUEST_CODE);

        } else {

            // Log.i(TAG, "已经全部打开权限");
//            updata();
        }


    }

//    private void updata() {
//        Location location = LocationUtils.getInstance(context).showLocation();
//        double lat = location.getLatitude();//维度
//        double lon = location.getLongitude();//经度
//
//        Log.d("location",""+lat+lon);
//
//        RequestParams pa = new RequestParams();
//        pa.put("taskname", bean.id);
//        pa.put("Action", "q");
//        pa.put("GPS_J_1", "" + lon);
//        pa.put("GPS_W_1", "" + lat);
//        Log.d("param", pa.toString());
//        try {
//            pa.put("photo1", new File(list.get(0).getCompressPath()));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        HttpUtil.httppost(URLConstants.UP, pa, new HttpUtil.SimpJsonHandle(context) {
//            @Override
//            public void onStart() {
//                super.onStart();
//                showDialog();
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//                super.onSuccess(statusCode, headers, response);
//                ToastHelper.shortToast(context, "上传成功");
//                finish();
//                Intent intent = new Intent(context, EndTaskActivity.class);
//                intent.putExtra("task", bean);
//                startActivity(intent);
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                super.onFailure(statusCode, headers, responseString, throwable);
//
//            }
//
//            @Override
//            public void onProgress(long bytesWritten, long totalSize) {
//                super.onProgress(bytesWritten, totalSize);
//
//            }
//
//            @Override
//            public void onFinish() {
//                super.onFinish();
//                dia.dismiss();
//            }
//        });


}
